import pygtk
pygtk.require('2.0')
import gtk
# import os, sy
import os, sys

from mokocard.util.sqlutil import SqlUtil
from mokocard.gui.mainpage import MainPage
from mokocard.gui.configpage import ConfigPage
from mokocard.gui.arrangepage import ArrangePage
from mokocard.gui.dbstatspage import DbStatsPage
from mokocard.obj.configdata import ConfigData
from mokocard.util.logutil import LogUtil

class MainWindow:

	# pages are
	# 0	mainpage
	# 1 configpage
	# 2 dbstatspage
	# 3 arrangepage

	sqlUtil = None
	
	# using to initialize page when changed if request
	# notebook is notebook
	# page is new current page
	# page_num index of new current page
	def changeTab(self, notebook, page, page_num):
		
		if page_num == 3:
			# arrange page
			# we can't use in pygtk page parameter (see documentation)
			notebook.get_nth_page(3).active(notebook.get_nth_page(0).counter)
			
	def delete_event(self, widget, event, data=None):
		# If you return FALSE in the "delete_event" signal handler,
		# GTK will emit the "destroy" signal. Returning TRUE means
		# you don't want the window to be destroyed.
		# This is useful for popping up 'are you sure you want to quit?'
		# type dialogs.
		LogUtil.writeLog("delete event occurred")
		
		# Change FALSE to TRUE and the main window will not be destroyed with a "delete_event".

		return False

	def destroy(self, widget, data=None):
		
		# close log file
		LogUtil.writeLog("destroy signal occurred")
		self.logUtil.closeLogFile()
		# close database
		self.sqlUtil.closeDatabase()
		

		gtk.main_quit()
		
	def buildLayout(self, configFile):
		
		configData = ConfigData(configFile)
		# print to video config data
		LogUtil.writeLog(configData.getPrintableConfigData())
		
		# from now we write data in logFile
		self.logUtil = LogUtil(configData.getPathLogFile(), configData.stdoutMode)
		
		notebook = gtk.Notebook()
		notebook.set_tab_pos(gtk.POS_TOP)
		notebook.show()
		self.show_tabs = True
		self.show_border = True
		
		# build table
		table = gtk.Table(1,1,False)
		table.show()
		self.window.add(table)
		table.attach(notebook,0,1,0,1)
		
		# prepare tabs		
		self.tabs = notebook
		self.tabs.set_property("homogeneous",True)
		
		self.sqlUtil = SqlUtil()
		# main page
		mainPage = MainPage(configData, self.sqlUtil)
		notebook.append_page(mainPage, gtk.Label("  Cards  "))
		# config page
		configPage = ConfigPage(configData, self.sqlUtil)
		notebook.append_page(configPage, gtk.Label("  Config "))
		# db statistic page
		dbStatsPage = DbStatsPage(configData, self.sqlUtil)
		notebook.append_page(dbStatsPage, gtk.Label(" DB Stats "))
		# arrange data page
		arrangePage = ArrangePage(configData, self.sqlUtil)
		notebook.append_page(arrangePage, gtk.Label(" Arrange "))
		
		notebook.connect("switch-page", self.changeTab)
		
		# setting sensitive buttons
		mainPage.disableButtons()
		
		configPage.setMainPage(mainPage)

		
	def __init__(self, configFile):		
		
		# create a new window
		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
	
		# When the window is given the "delete_event" signal (this is given by the window manager, usually by the "close" option, or on the titlebar), we ask it to call the delete_event () function as defined above. 
		# The data passed to the callback function is NULL and is ignored in the callback function.
		self.window.connect("delete_event", self.delete_event)
	
		# Here we connect the "destroy" event to a signal handler.  
		# This event occurs when we call gtk_widget_destroy() on the window, or if we return FALSE in the "delete_event" callback.
		self.window.connect("destroy", self.destroy)
	
		# Sets the border width of the window.
		self.window.set_border_width(10)
		
		# title
		self.window.set_title("MokoCard")
		
		# size
		self.window.set_default_size(480,640)
		
		self.buildLayout(configFile)
		
		self.window.show()
		
	def main(self):
        
		# All PyGTK applications must have a gtk.main(). Control ends here and waits for an event to occur (like a key press or mouse event).
		gtk.main()
